<?php
/**
 * create to 2021/6/24 20:49
 */

namespace app\mainadmin\controller\Liveing;

use app\mainadmin\model\LiveGoodsModel;
use app\AdminController;
use app\shop\model\GoodsModel;
use think\Db;
use think\facade\Session;

/**
 * 直播间商品配置
 * Class Index
 * @package app\store\controller
 */
class Goods extends AdminController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize(){
        parent::initialize();
        $this->Model = new LiveGoodsModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index() {
        if (input("p/d")&&input("p/d")>0) {
            return $this->getList(false);
        }
        $this->getList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
        $where = [];

        $viewObj = $this->Model->alias('lg')->join("shop_goods sg", 'lg.goods_id=sg.goods_id', 'left')->order("id desc")->field('lg.*,sg.goods_name,sg.shop_price');
        $data = $this->getPageList($this->Model,$viewObj);
        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('mainadmin@liveing/goods/list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 详情
    /*------------------------------------------------------ */
    public function info() {
        $live_goods = input('id',0,'intval');
        if ($live_goods < 1){
            return $this->error('传参错误.');
        }
        if ($this->request->isPost()) {
            $save_data = input('post.');
            $res = $this->Model->where('id',$live_goods)->update($save_data);
            if ($res == false) return $this->error();
            return $this->success('修改成功.');
        }
        $row = $this->Model->alias('lg')->join("shop_goods sg", 'lg.goods_id=sg.goods_id', 'left')->where('lg.id',$live_goods)->field('lg.*,sg.goods_name,sg.shop_price')->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $this->assign('row',$row->toArray());
        return $this->fetch('info');
    }
    /*------------------------------------------------------ */
    //-- 删除
    /*------------------------------------------------------ */
    public function del() {
        $live_goods = input('id',0,'intval');
        if ($live_goods < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id',$live_goods)->delete();
        if (!$row){
            return $this->error('删除失败.');
        } else {
            return $this->success('删除成功.');
        }
    }
    /*------------------------------------------------------ */
    //-- 开始|结束讲解商品
    /*------------------------------------------------------ */
    public function explain() {
        $live_goods = input('id',0,'intval');
        if ($live_goods < 1){
            return $this->error('传参错误.');
        }
        $status = input('status',0,'intval');
        Db::startTrans();
        if ($status==1) {
            $this->Model->where(['is_explained'=>1])->update(['is_explained'=>0]);
        }
        $row = $this->Model->where('id',$live_goods)->update(['is_explained'=>$status]);
        if ($row) {
            Db::commit();
            return $this->success('操作成功.');
        } else {
            Db::rollback();
            return $this->success('操作失败.');
        }
    }
    /*------------------------------------------------------ */
    //-- 选择商品
    /*------------------------------------------------------ */
    public function choose() {
        if ($this->request->isPost()) {
            $choose_ids = input("choose_ids");
            if (count($choose_ids)<=0){
                return $this->error('请选择商品');
            }
            $save_data = [];
            foreach ($choose_ids as $v) {
                $goods_info = GoodsModel::where('goods_id',$v)->field('shop_price')->find();
                $add_data = [
                    'goods_id'=>$v,
                    'live_price'=>"",
                    'stock'=>'',
                    'is_explained'=>0
                ];
                $save_data[] = $add_data;
            }
            if ($this->Model->insertAll($save_data)) {
                return $this->success('添加成功');
            } else {
                return $this->error('添加失败，请重新选择');
            }
        }
        $this->getChooseList(true);
        return $this->fetch();
    }
    /*------------------------------------------------------ */
    //-- 商品数据
    /*------------------------------------------------------ */
    public function getChooseList($runData = false) {
        if (empty($page_size)){
            $page_size = input("page_size/d",0);
            $session_page_size = Session::get('page_size') * 1;
            if ($page_size <= 1 ){
                if ($session_page_size <= 1) $session_page_size = 20;
                $page_size = $session_page_size;
            }
            elseif ($page_size != $session_page_size) Session::set('page_size',$page_size);
        }
        $goodsModel = new GoodsModel();
        $where = ['isputaway'=>1];
        $goods_list = $this->Model->group('goods_id')->field('goods_id')->select()->toArray();
        $ids = [];
        foreach ($goods_list as $v) {
            $ids[] = $v['goods_id'];
        }
        $data = $goodsModel->getPageList(input("page", 1),$where,'goods_id,goods_thumb,goods_name',$page_size,'',$ids);
        $this->assign('data',$data);

        if ($runData == false){
            $data['content']= $this->fetch('choosegoodslist')->getContent();
            $data['page_content']= $this->fetch('choosepage')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return $data;
    }
}